const { Sequelize } = require('sequelize');
const config = require('@/config/config.default.js');

const { MYSQL_HOST, MYSQL_DB, MYSQL_USER, MYSQL_PASSWORD } = config; // 解构赋值获取数据库配置

const sequelize = new Sequelize(MYSQL_DB, MYSQL_USER, MYSQL_PASSWORD, {
  host: MYSQL_HOST,
  dialect: 'mysql',
  timezone: '+08:00',
  // 启用并优化连接池配置
  pool: {
    max: 20,     // 最大连接数
    min: 5,      // 最小连接数
    acquire: 60000, // 获取连接超时时间
    idle: 30000,  // 连接空闲时间
    evict: 15000 // 检测连接空闲时间
  }
});

// 测试数据库连接
sequelize
  .authenticate() // 验证数据库连接
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch((err) => {
    console.error('Unable to connect to the database:', err);
  });

  module.exports = sequelize;
